import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.awt.event.*;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.border.EtchedBorder;


public class GoalsWindow extends JFrame implements ActionListener, SavedEventGenertor {
	
	private ArrayList<SavedListener> savedListeners = 
		new ArrayList<SavedListener>();
	
	Bridget bridget;
	
	private JLabel currentWeightLabel;
	private JLabel idealWeightLabel;
	private JTextField desiredWeightField;
	private JTextField fitnessPlanField;
	private JButton saveButton;

	public GoalsWindow(Bridget bridget) {
		super("Goals");
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		
		this.bridget = bridget;
		
		JPanel inputPanel = new JPanel();
		inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.PAGE_AXIS));
		//JPanel labelsPanel = new JPanel();
		//labelsPanel.setLayout(new GridLayout(0, 2));
		//labelsPanel.add(new JLabel("Your current weight, kg: "));
		currentWeightLabel = new JLabel("Your current weight, kg: 0");
		currentWeightLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		inputPanel.add(currentWeightLabel);
		idealWeightLabel = new JLabel("Ideal weight for you, kg: 0");
		idealWeightLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		inputPanel.add(idealWeightLabel);	
		
		//inputPanel.add(labelsPanel);
		
		inputPanel.add(Box.createRigidArea(new Dimension(5,10)));

		
		JLabel label;
		label = new JLabel("Enter your desired weight, kg:", JLabel.LEFT);
		label.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		inputPanel.add(label);
		desiredWeightField = new JTextField("0");
		//Font font = desiredWeightField.getFont();
		//font.deriveFont(18);
		Font font = new Font("Arial", Font.PLAIN, 15);
		//txt.setFont(font);
		desiredWeightField.setFont(font);
		desiredWeightField.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
		//desiredWeightField.setMinimumSize(new Dimension(10, 100));
		inputPanel.add(desiredWeightField);
		label = new JLabel("Your fitness plan, kcal:", JLabel.LEFT);
		label.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		inputPanel.add(label);
		fitnessPlanField = new JTextField("0");
		fitnessPlanField.setFont(font);
		fitnessPlanField.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
		inputPanel.add(fitnessPlanField);
		
		JPanel mainPanel = new JPanel();
		mainPanel.setLayout(new BorderLayout());
		mainPanel.add(inputPanel, BorderLayout.NORTH);
		saveButton = new JButton("Save data");
		saveButton.setActionCommand("save clicked");
		saveButton.addActionListener(this);
		mainPanel.add(saveButton, BorderLayout.SOUTH);
		mainPanel.setPreferredSize(new Dimension(250, 300));
		
		updateFields();
		
		setContentPane(mainPanel);
		pack();
	}
	
	public void updateFields() {
		currentWeightLabel.setText("Your current weight, kg : " +
				String.valueOf(bridget.getWeight()));
		
		idealWeightLabel.setText  ("Your ideal weight, kg   : " +
				String.valueOf(bridget.getIdealWeight()));
		
		desiredWeightField.setText(String.valueOf(bridget.getWeightgoal()));
		fitnessPlanField.setText(String.valueOf(bridget.getFitgoal()));
		
		
	}

	public void addSavedListener(SavedListener listener) {
		savedListeners.add(listener);
		
	}
	
	private void fireSavedEvent(SavedEvent e) {
		int j = savedListeners.size();
		if (j==0) { return; }
		for(int i=0; i<j; i++) {
			savedListeners.get(i).saved(e);
		}
	}

	public void actionPerformed(ActionEvent e) {
			if ("save clicked".equals(e.getActionCommand())) {
				
				double newDesiredWeight = Double.parseDouble(desiredWeightField.getText()); 
				if ( newDesiredWeight != bridget.getWeightgoal() ) {
					bridget.setWeightgoal(newDesiredWeight);
				}
				
				double newFitnessPlan = Double.parseDouble(fitnessPlanField.getText()); 
				if ( newFitnessPlan != bridget.getFitgoal() ) {
					bridget.setFitgoal(newFitnessPlan);
				}
			
				SavedEvent event = new SavedEvent(this);
				event.name = "goals";
				this.fireSavedEvent(event);
			
		} 
		
	}


}
